///<reference path='jquery.d.ts' />

function updateGrid(url: string, containerId: string, orderBy: string) {
    $.ajax({
        url: url,
        data: {
            ContainerId: containerId,
            OrderBy: orderBy
        },
        cache: false,
        success: data => {
            $("#" + containerId).html(data);
        },
        error: (jqXHR, textStatus, errorThrow) => {
            alert("Request failed: " + textStatus + ", " + errorThrow);
        }
    });
}

function te006(url: string) {
    $(".Expand_te001").live("click", function () {
        var expandIcon = $(this);
        var node = expandIcon.parent();
        if (node.hasClass("ExpandLeaf_te003")) return;
        if (node.hasClass("ExpandOpen_te004"))
            setOpeningClass(node, "ExpandClosed_te005");
        else if (node.hasClass("ExpandClosed_te005")) {
            if (!node.data("isLoaded_te011"))
                loadNodes(url, node, expandIcon, function () {});
            else
                setOpeningClass(node, "ExpandOpen_te004");
        } else
            throw new Error("invalid opening class");
    });
    $(".Content_te012").live("click", function () {
        selectNode($(this));
    });
}

function selectNode(content: JQuery) {
    $(".Selected_te016").removeClass("Selected_te016");
    content.addClass("Selected_te016");
}

function setOpeningClass(node: JQuery, newClass: string) {
    $.each(["ExpandOpen_te004", "ExpandClosed_te005", "ExpandLeaf_te003"], function (index, value) {
        return node.removeClass(value);
    });
    node.addClass(newClass);
}

function loadNodes(url: string, node: JQuery, expandIcon: JQuery, afterSuccess: () => void) {
    expandIcon.removeClass("Expand_te001");
    expandIcon.addClass("ExpandLoading_te010");
    $.ajax({
        url: url,
        data: {
            id_te009: node.attr("data-id_te008")//qwe
        },
        cache: false,
        success: function (data) {
            node.children("ul").remove();
            node.append(data);
            if (node.children("ul").children().length === 0)
                setOpeningClass(node, "ExpandLeaf_te003");
            else
                setOpeningClass(node, "ExpandOpen_te004");
            node.data("isLoaded_te011", true);
            afterSuccess();
        },
        error: function (jqXHR, textStatus, errorThrow) {
            alert("Request failed: " + textStatus + ", " + errorThrow);
        },
        complete: function () {
            expandIcon.removeClass("ExpandLoading_te010");
            expandIcon.addClass("Expand_te001");
        }
    });
}

function jumpTo_te017(url: string, nodeIds: string[]) {
    loadedTreeWalk(url, nodeIds, 0, null);
}

function loadedTreeWalk(url: string, nodeIds: string[], index: number, selectorContext: JQuery) {
    var node = getNode(nodeIds[index], selectorContext);
    if (index >= nodeIds.length - 1)
        selectNode(node.children(".Content_te012"));
    else if (node.data("isLoaded_te011") !== true) {
        loadNodesRecursively(url, nodeIds, index, node);
    } else {
        if (node.hasClass("ExpandClosed_te005"))
            setOpeningClass(node, "ExpandOpen_te004");
        loadedTreeWalk(url, nodeIds, index + 1, node);
    }
}

function loadNodesRecursively(url: string, nodeIds: string[], index: number, node: JQuery) {
    loadNodes(url, node, node.children(".Expand_te001"), function() {
        var newIndex = index + 1;
        var newNode = getNode(nodeIds[newIndex], node);
        if (newIndex >= nodeIds.length - 1)
            selectNode(newNode.children(".Content_te012"));
        else
            loadNodesRecursively(url, nodeIds, newIndex, newNode);
    });
}

function getNode(id: string, selectorContext: JQuery) {
    return $("[data-id_te008='" + id + "']:first", selectorContext);
}